package com.yang.webapi.module.controller;

import com.yang.service.model.dto.post.AddrPostDTO;
import com.yang.service.model.dto.put.AddrPutDTO;
import com.yang.service.model.vo.AddrVo;
import com.yang.webapi.module.service.AddrWebService;
import com.zhy.base.webapi.annoation.ResponseFormatter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@ResponseFormatter
@RequestMapping(value = "addr")
@CrossOrigin
@Slf4j
@Api(tags = "地址管理", description = "AddrController")
public class AddrController {

    @Autowired
    private AddrWebService addrWebService;

    @ApiOperation("保存地址")
    @PostMapping
    public String save(@RequestBody AddrPostDTO dto) {
       return addrWebService.save(dto);
    }

    /**
     * 所以：
     * 这个userID是不需要传的，直接通过token解析到UserUtils中了
     * 就是这样的
     */
    @ApiOperation("根据用户获取所有的地址")
    @GetMapping("list")
    public List<AddrVo> getAllAddr(){
        return addrWebService.listAllAddrByUserId();
    }


    @ApiOperation("根据地址Id删除")
    @DeleteMapping("{addId}")
    public boolean deleteAddByAddId(@PathVariable(value = "addId") String addId){
        return addrWebService.deleteAddByAddrId(addId);
    }

    @ApiOperation("根据地址Id更新地址")
    @PutMapping("{addId}")
    public boolean putAddrById(@PathVariable(value = "addId") String addId,
                               @RequestBody AddrPutDTO dto){
        return addrWebService.updateAddrById(addId,dto);
    }
}
